<?php
require_once(ROOTDIR.'classes/basedb.php');

class PormotionDB extends BaseDB{
	public function insertPromotion($fieldsData){
		$fieldsData = $this->_checkPromotionData($fieldsData);
		if (!$fieldsData) return null;
		$sql = "INSERT INTO fanwe_promotion SET " . $this->_getUpdateSqlString($fieldsData);
		//echo $sql.'\n';
		$this->_db_writer->update($sql);
	}
	
	public function checkPromotionExist($band_name,$goods_title,$baidu_url){
		$sql = "select id from fanwe_promotion where brand_name='".$brand_name."' and goods_title='".$goods_title."' and baidu_url='".$baidu_url."'";
		$result = $this->_db_reader->get_one($sql);
		if($result){
			return $result['id'];
		}else{
			return 0;
		}		
	}
	
	public function getSortPromotionList($sort){
		$sql = "select * from fanwe_promotion where status=1 and sort='".$sort."' order by price desc limit 0,15";
		$query = $this->_db_reader->query($sql);
		$result = $this->_getAllResultFromQuery($query);
		return $result;		
	}
	
	public function getSortPromotionAllList($sort){
		$sql = "select * from fanwe_promotion where status=1";
		$query = $this->_db_reader->query($sql);
		$result = $this->_getAllResultFromQuery($query);
		return $result;
	}	
	
	public function getSendPromotionList(){
		$sql = "SELECT * 
					FROM `fanwe_promotion` 
					WHERE goods_url LIKE '%tmall%' and status=0";
		$query = $this->_db_reader->query($sql);
		$result = $this->_getAllResultFromQuery($query);
		return $result;
	}
	
	public function updatePromotionSort($id,$sort){
		$sql = "update fanwe_promotion set sort='".$sort."' where id=".$id;
		$this->_db_writer->update($sql);		
	}
	
	public function updatePromotionData($id,$baidu_url,$weibo_url){
		$sql = "update fanwe_promotion set baidu_url='".$baidu_url."',weibo_url='".$weibo_url."',status=1 where id=".$id;
		$this->_db_writer->update($sql);
	}
	
	public function updatePromotionBaidu($id,$baidu_url){
		$sql = "update fanwe_promotion set baidu_url='".$baidu_url."' where id=".$id;
		$this->_db_writer->update($sql);
	}
	
	private function _checkPromotionData($data){
		if (!is_array($data) || !count($data)) return null;
		$data = $this->_checkAllowField($data,$this->getPromotionStruct());
		return $data;
	}
	
	public function getPromotionStruct(){
		return array('brand_name','goods_title','goods_url','price','off_price','baidu_url','weibo_url','sort','holiday');
	}
	
	public function getMailList(){
		$sql = "select * from fanwe_mail";
		$query = $this->_db_reader->query($sql);
		$result = $this->_getAllResultFromQuery($query);
		return $result;
	}
	
	public function insertMail($fieldsData){
		$fieldsData = $this->_checkMailData($fieldsData);
		if (!$fieldsData) return null;
		$sql = "INSERT INTO fanwe_mail SET " . $this->_getUpdateSqlString($fieldsData);
		$this->_db_writer->update($sql);
	}
	
	private function _checkMailData($data){
		if (!is_array($data) || !count($data)) return null;
		$data = $this->_checkAllowField($data,$this->getMailStruct());
		return $data;
	}
	
	public function getMailStruct(){
		return array('address');
	}
}
?>